//
//  AddPhoneViewController.m
//  OperationPro
//
//  Created by yaxue wang on 2017/1/6.
//  Copyright © 2017年 展鸿科技. All rights reserved.
//

#import "AddPhoneViewController.h"
#import "ConfirmPhoneView.h"
#import "EditPhonePollingViewController.h"
#import "EditProduceViewController.h"
@interface AddPhoneViewController ()<UITextFieldDelegate>
@property(nonatomic,strong)CTextField *phoneText;
@property(nonatomic,strong)CTextField *nameText;
@property(nonatomic,strong)UIView *addView;

@end

@implementation AddPhoneViewController
- (void)viewDidDisappear:(BOOL)animated
{
    [super viewDidDisappear:animated];
    _phoneText.text = @"";
    _nameText.text = @"";

}
- (void)viewDidLoad {
    [super viewDidLoad];
    [self setTitle:@"添加联系人"];
    _addView= [[UIView alloc] initWithFrame:CGRectMake(0, 74, WindowWidth, WindowHeight-74)];
    _addView.backgroundColor=[UIColor whiteColor];
    [self.view addSubview:_addView];

    _phoneText =[[CTextField alloc]initWithFrame:CGRectMake(15, 0, _addView.bounds.size.width-30, 50)];
    _phoneText.placeholder =@"联系人手机号";
    _phoneText.delegate=self;
    [_phoneText setNullMsg:@"请输入11位手机号!"];
    _phoneText.font =F(15);
    [_phoneText setInputType:CTextFieldInputTypePhone];
    _phoneText.tag =100;
    [_addView addSubview:_phoneText];
    UILabel *line1=[[UILabel alloc]initWithFrame:CGRectMake(15, CGRectGetMaxY(_phoneText.frame), _addView.bounds.size.width-30, 0.5)];
    line1.backgroundColor=LineColor;
    [_addView addSubview:line1];
    
    _nameText =[[CTextField alloc]initWithFrame:CGRectMake(15, CGRectGetMaxY(line1.frame), _addView.bounds.size.width-30, 50)];
    _nameText.placeholder =@"联系人姓名";
    _nameText.font =F(15);
    [_nameText setInputType:CTextFieldInputTypeDefault];
    [_addView addSubview:_nameText];
    UILabel *line2=[[UILabel alloc]initWithFrame:CGRectMake(15, CGRectGetMaxY(_nameText.frame), _addView.bounds.size.width-30, 0.5)];
    line2.backgroundColor=LineColor;
    [_addView addSubview:line2];
    UIButton*confirmBtn =[UIButton buttonWithType:UIButtonTypeCustom];
    [_addView addSubview:confirmBtn];
    confirmBtn.frame=CGRectMake(29, CGRectGetMaxY(line2.frame)+40, WindowWidth-58, 40);
    [confirmBtn setBackgroundColor:MainColor];
    confirmBtn.layer.masksToBounds=YES;
    confirmBtn.layer.cornerRadius=8;
    [confirmBtn setTitle:@"确定" forState:UIControlStateNormal];
   
    [confirmBtn bk_addEventHandler:^(id sender) {
        if (!_phoneText.verfiy) {
            
            return;
        }
        if (_nameText.text.length<=0) {
        [CClass showMessage:@"请输入联系人姓名" completion:^{
            
        }];
        return;
        }
        [self.view endEditing:YES];

        if (self.mobileAr.count>0) {
            NSMutableDictionary *dic=[[NSMutableDictionary alloc]init];
            dic =[self.mobileAr objectAtIndex:0];
            if([_phoneText.value isEqualToString:[dic objectForKey:@"mobile"]])
            {
                [CClass showMessage:@"不能添加相同的联系人！"];
                return;
            }
        }
        if (self.mobileAr.count>2) {
            [CClass showMessage:@"最多添加两个联系人"];
            return;
        }else
        {
            NSMutableDictionary *dataDic=[[NSMutableDictionary alloc]init];
            [dataDic setObject:_phoneText.value forKey:@"mobile"];
            [dataDic setObject:_nameText.text forKey:@"name"];
            [self.mobileAr addObject:dataDic];
        }
        

        EditProduceViewController *editVc=[[EditProduceViewController alloc]init];
        editVc.Uuid=self.Uuid;
        editVc.mobileAr=self.mobileAr;
        editVc.phoneString=_phoneText.value;
        editVc.isHasPhone=self.isHasPhone;
        [self pushController:editVc];

        
    } forControlEvents:UIControlEventTouchUpInside];
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    [self.view endEditing:YES];
}

-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    NSString *shouldBeString = [textField.text stringByReplacingCharactersInRange:range withString:string];
    if (textField==_phoneText) {
        if (shouldBeString.length==13) {
            NSString *v = shouldBeString;
            
            v = [shouldBeString stringByReplacingOccurrencesOfString:@" " withString:@""];
            
            //通过电话发送请求找到姓名 还没有判断注册没有
            [CClass HttpRequestTask:YES withUrl:@"customer_findUser" withData:@{@"mobile":v} notifyError:YES onSuccess:^(BaseModel *model) {
                if ([[model.B_data allKeys] containsObject:@"name"]) {
                    _nameText.userInteractionEnabled=NO;
                    _nameText.text=[model.B_data objectForKey:@"name"];
                    [self.view endEditing:YES];
                    
                }else
                {
                    _nameText.text=@"";
                    _nameText.userInteractionEnabled=YES;
                    
                }
                
                
            } onError:^(BaseModel *model) {
                _nameText.text=@"";
                _nameText.userInteractionEnabled=YES;
            }];
            
        }else
        {
            _nameText.text=@"";
            _nameText.userInteractionEnabled=YES;
        }
    }else
    {
        if ([[[UITextInputMode currentInputMode]primaryLanguage] isEqualToString:@"emoji"]) {
            return NO;
        }
    }
    
        
return YES;

}

@end
